package com.ll.controller;

import com.sun.jersey.api.client.Client;
import com.sun.jersey.api.client.WebResource;
import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileUploadException;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.util.List;
import java.util.UUID;

/**
 * 2020/9/13 - 14:39
 */
@Controller
@RequestMapping("/user")
public class UserController {

    /**
     * 跨服务器上传
     * @param upload
     * @return
     * @throws Exception
     */
    @RequestMapping("/fileUpload3")
    public String fileUpload3(MultipartFile upload) throws Exception {
        System.out.println("UserController => fileUpload3");

        //定义一个上传服务器路径
        String path = "http://localhost:9090/uploads/";

        //上传文件项
        //上传文件名
        String filename = upload.getOriginalFilename();
        //把文件的名称设置成唯一值 uuid
        String uuid = UUID.randomUUID().toString().replace("-", "");
        filename = uuid + "_" + filename;

        //完成文件上传,跨服务器上传
        //创建客户端对象
        Client client = Client.create();

        //和图片服务器进行连接
        WebResource webResource = client.resource(path + filename);

        //上传文件
        webResource.put(upload.getBytes());

        return "success";
    }

    /**
     * SpringMVC文件上传
     *
     * @param request
     * @return
     * @throws Exception
     */
    @RequestMapping("/fileUpload2")
    public String fileUpload2(HttpServletRequest request, MultipartFile upload) throws Exception {
        System.out.println("UserController => fileUpload2");

        //使用fileupload组件完成上传
        //上传的位置
        String path = request.getSession().getServletContext().getRealPath("/uploads/");
        //判断,该路径是否存在
        File file = new File(path);
        if (!file.exists()) {
            //创建文件夹
            file.mkdirs();
        }

        //上传文件项
        //上传文件名
        String filename = upload.getOriginalFilename();
        //把文件的名称设置成唯一值 uuid
        String uuid = UUID.randomUUID().toString().replace("-", "");
        filename = uuid + "_" + filename;
        //完成文件上传
        upload.transferTo(new File(path, filename));

        return "success";
    }

    /**
     * 文件上传
     *
     * @return
     */
    @RequestMapping("/fileUpload1")
    public String fileUpload1(HttpServletRequest request) throws Exception {
        System.out.println("UserController => fileUpload1");

        //使用fileupload组件完成上传
        //上传的位置
        String path = request.getSession().getServletContext().getRealPath("/uploads/");

/*        System.out.println("request.getSession(): " + request.getSession());
        System.out.println("request.getSession().getServletContext(): " + request.getSession().getServletContext());
        System.out.println("path: " + path);*/

        //判断,该路径是否存在
        File file = new File(path);
        if (!file.exists()) {
            //创建文件夹
            file.mkdirs();
        }

        //解析request对象,获取到上传文件项
        DiskFileItemFactory factory = new DiskFileItemFactory();
        ServletFileUpload upload = new ServletFileUpload(factory);

        //解析request
        List<FileItem> items = upload.parseRequest(request);

        //遍历
        for (FileItem item : items) {
            //进行判断,当前item是否是上传文件项
            if (item.isFormField()) {
                //普通的表单项
            } else {
                //上传文件项
                //上传文件名
                String filename = item.getName();
                //把文件的名称设置成唯一值 uuid
                String uuid = UUID.randomUUID().toString().replace("-", "");
                filename = uuid + "_" + filename;
                //完成文件上传
                item.write(new File(path, filename));
                //删除临时文件
                item.delete();
            }
        }

        return "success";
    }
}
